package com.example.homeworkmanagement.controller;


import com.example.homeworkmanagement.dto.AssignmentsAndTeachersAndCoursesAndTeachersAndSubmissionsDTO;
import com.example.homeworkmanagement.dto.AssignmentsAndTeachesAndCoursesAndTeachersDTO;
import com.example.homeworkmanagement.pojo.Assignments;
import com.example.homeworkmanagement.service.AssignmentsService;
import com.example.homeworkmanagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 拒绝加班
 * @since 2025-05-25
 */
@RestController
@RequestMapping("/assignments")
@Api(tags="教师作业管理",description="提供管理API接口")
@CrossOrigin
public class AssignmentsController {
    @Autowired
    private AssignmentsService assignmentsService;
    /**
     * 根据作业ID获取详情
     */
    @GetMapping("/getAssignmentDetail")
    @ApiOperation(value = "根据作业ID查询详情")
    @ApiImplicitParam(name = "assignmentId", value = "作业ID（主键）", required = true, dataType = "integer")
    public ResultVO<Assignments> getAssignmentDetail(Integer assignmentId) {
        return assignmentsService.getAssignmentById(assignmentId);
    }

    @ApiOperation(value = "获取教师作业列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", dataType = "integer"),
            @ApiImplicitParam(name = "teachId", value = "教学ID", dataType = "integer"),
            @ApiImplicitParam(name = "title", value = "作业标题", dataType = "string"),
            @ApiImplicitParam(name = "content", value = "作业内容", dataType = "string"),
            @ApiImplicitParam(name = "startTime", value = "开始时间（格式：yyyy-MM-dd HH:mm:ss）", dataType = "string"),
            @ApiImplicitParam(name = "dueTime", value = "截止时间（格式：yyyy-MM-dd HH:mm:ss）", dataType = "string"),
            @ApiImplicitParam(name = "state", value = "作业状态(1-未批改,2-已批改)相对于教师", dataType = "string"),
            @ApiImplicitParam(name = "courseId", value = "课程ID", dataType = "string"),
            @ApiImplicitParam(name = "teacherId", value = "教师ID", dataType = "string"),
            @ApiImplicitParam(name = "teacherName", value = "教师姓名", dataType = "string"),
            @ApiImplicitParam(name = "courseName", value = "课程名称", dataType = "string"),
            @ApiImplicitParam(name = "classIds", value = "班级ID列表（逗号分隔）", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码（从1开始）", dataType = "integer", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页数量", dataType = "integer", defaultValue = "10")
    })
    @GetMapping("/selectAssignmentByInfos")
    public ResultVO<List<AssignmentsAndTeachesAndCoursesAndTeachersDTO>> selectAssignmentByInfos(AssignmentsAndTeachesAndCoursesAndTeachersDTO dto, Integer page, Integer size
    ) {
        if (page == null || page <= 0) {
            page = 1;
        }
        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        return assignmentsService.selectAssignmentByInfos(dto, page, size);
    }

    @ApiOperation(value = "获取学生作业列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", dataType = "integer"),
            @ApiImplicitParam(name = "teachId", value = "教学ID", dataType = "integer"),
            @ApiImplicitParam(name = "title", value = "作业标题", dataType = "string"),
            @ApiImplicitParam(name = "content", value = "作业内容", dataType = "string"),
            @ApiImplicitParam(name = "startTime", value = "开始时间（格式：yyyy-MM-dd HH:mm:ss）", dataType = "string"),
            @ApiImplicitParam(name = "dueTime", value = "截止时间（格式：yyyy-MM-dd HH:mm:ss）", dataType = "string"),
            @ApiImplicitParam(name = "state", value = "作业状态(1-未批改,2-已批改)相对于教师", dataType = "string"),
            @ApiImplicitParam(name = "courseId", value = "课程ID", dataType = "string"),
            @ApiImplicitParam(name = "teacherId", value = "教师ID", dataType = "string"),
            @ApiImplicitParam(name = "teacherName", value = "教师姓名", dataType = "string"),
            @ApiImplicitParam(name = "courseName", value = "课程名称", dataType = "string"),
            @ApiImplicitParam(name = "classIds", value = "班级ID列表（逗号分隔）", dataType = "string"),
            @ApiImplicitParam(name = "studentId", value = "学生id", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码（从1开始）", dataType = "integer", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页数量", dataType = "integer", defaultValue = "10")
    })
    @PostMapping("/selectAssignmentAndSubmissionByInfos")
    public ResultVO<List<AssignmentsAndTeachersAndCoursesAndTeachersAndSubmissionsDTO>> selectAssignmentAndSubmissionByInfos(AssignmentsAndTeachesAndCoursesAndTeachersDTO dto,String studentId, Integer page, Integer size
    ) {
        if (page == null || page <= 0) {
            page = 1;
        }
        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        return assignmentsService.selectAssignmentAndSubmissionByInfos(dto,studentId, page, size);
    }

    /**
     * 删除作业
     */
    @DeleteMapping("/deleteAssignment")
    @ApiOperation("根据作业ID删除作业")
    @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "integer")
    public ResultVO<Assignments> deleteAssignment(Integer assignmentId) {
        return assignmentsService.deleteAssignment(assignmentId);
    }

    @PostMapping("/uploadAssignment")
    @ApiOperation("发布作业及上传附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "teachId", value = "教学ID", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "title", value = "作业标题", required = true, dataType = "string"),
            @ApiImplicitParam(name = "content", value = "作业内容", required = false, dataType = "string"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "date"),
            @ApiImplicitParam(name = "dueTime", value = "截止时间", required = false, dataType = "date"),
            @ApiImplicitParam(name = "state", value = "作业状态(1-未批改,2-已批改)", required = false, dataType = "string"),
            @ApiImplicitParam(name = "file", value = "作业附件", required = false, dataType = "multipartFile")
    })
    public ResultVO<Assignments> uploadAssignment(Integer assignmentId, Integer teachId,String title,String content, Date startTime,Date dueTime, String state, MultipartFile file) {
        return assignmentsService.uploadAssignment(assignmentId,teachId,title,content,startTime,dueTime,state,file);
    }

    @PostMapping("/updateAssignment")
    @ApiOperation("更新作业及更新附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "teachId", value = "教学ID", required = false, dataType = "integer"),
            @ApiImplicitParam(name = "title", value = "作业标题", required = false, dataType = "string"),
            @ApiImplicitParam(name = "content", value = "作业内容", required = false, dataType = "string"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "date"),
            @ApiImplicitParam(name = "dueTime", value = "截止时间", required = false, dataType = "date"),
            @ApiImplicitParam(name = "state", value = "作业状态(1-未批改,2-已批改)", required = false, dataType = "string"),
            @ApiImplicitParam(name = "file", value = "作业附件", required = false, dataType = "multipartFile")
    })
    public ResultVO<Assignments> updateAssignment(Integer assignmentId, Integer teachId,String title,String content, Date startTime,Date dueTime, String state, MultipartFile file) {
        return assignmentsService.updateAssignment(assignmentId,teachId,title,content,startTime,dueTime,state,file);
    }

}
